<?php


namespace app\common\model;

use app\model\AgentServiceConfig;
use app\common\facade\auth\Auth;
use app\common\service\auth\AuthAdminService;
use app\common\service\auth\AuthAgentService;

class Search
{
    /**
     * 代理财务明细搜索框
     * @return \think\response\Json
     * @author ymzhang
     * @access public
     * @date 2022-08-11 20:40
     */
    public function getDropdown($type)
    {
        $from = Auth::from();
        if (!in_array($type, [0, 1, 2, 3, 4])) {
            return fail('查询类别错误');
        }

        $balance_types = BALANCE_TYPES;
        //获取权限
        $obj = new AgentServiceConfig();
        $serv_types = $obj->getAllServOrHistory();
        $serv_types = array_keys($serv_types);
        //$serv_types = [1,3];
        $list = array();
        if ($type == 1) { //预付扣款
            $list = array_filter($balance_types, function($t) use ($type) {
                if (isset($t['prepaid_debit']) && $t['prepaid_debit'] == 1) {
                    return $t['prepaid_debit'] == 1;
                }
            });
        } elseif($type == 2) { //充值记录
            $list = array_filter($balance_types, function($t) use ($type) {
                if (isset($t['recharge_record']) && $t['recharge_record'] == 1) {
                    return $t['recharge_record'] == 1;
                }
            });
        } elseif($type == 3) { //佣金明细
            $list = array_filter($balance_types, function($t) use ($type) {
                if (isset($t['commission_details']) && $t['commission_details'] == 1) {
                    return $t['commission_details'] == 1;
                }
            });
        } elseif($type == 4) {//提现记录
            $list = array_filter($balance_types, function($t) use ($type) {
                if (isset($t['embody_record']) && $t['embody_record'] == 1) {
                    return $t['embody_record'] == 1;
                }
            });
        } else { //0
            foreach ($balance_types as $k => $value) {
                $balance_types[$k]['id'] = $k;
            }
            return success('success', array_values($balance_types));
        }
        if (in_array($type, [1, 3])) {
            foreach ($list as $key => $prepaid_debit) {
                if ($from == 'agent') {
                    if(!in_array($prepaid_debit['serv_type'], $serv_types)) {
                        unset($list[$key]);
                        continue;
                    }
                }
                $list[$key]['id'] = $key;
            }
        } else {
            foreach ($list as $k => $value) {
                $list[$k]['id'] = $k;
            }
        }
        return success('success', array_values($list));
    }
}